//
//  MeterRequest.swift
//  Employee
//
//  Created by tancheng on 2018/6/21.
//  Copyright © 2018年 remotevillage. All rights reserved.
//

import Foundation
import SwiftyJSON
import Moya

struct MeterRequest: DataResultProtocol {
    
    static let shared = MeterRequest()
    
    // MARK: - 添加仪表
    @discardableResult
    func addMeter(send:AddMeterSend, success: @escaping () -> (), failure: @escaping VoidClosure) -> Cancellable {
        
        let requestKey = SJApp.functionKey()
        if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
        let cancle = MeterProvider.request(MeterAPI.addMeter(send: send)) { (result) in
            
            GlobalConfig.requestCache.removeValue(forKey: requestKey)
            
            self.dataProtocol(with: result, success: { (data) in
                
                success()
                
            }, overdue: {
                let _ = MeterRequest.shared.addMeter(send: send ,success: success, failure: failure)
                
            }, failure: failure)
            
        }
        GlobalConfig.requestCache[requestKey] = cancle
        
        return cancle
        
    }
    
    
    //MARK: 获取抄表任务列表
    @discardableResult
    func getReadingMeterTaskList(send:ReadingMeterTaskSend, success: @escaping ([ReadingMeterTaskModel],PageModel) -> (), failure: @escaping VoidClosure) -> Cancellable {
        
        let requestKey = SJApp.functionKey()
        if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
        let cancle = MeterProvider.request(MeterAPI.getReadingMeterTaskList(send: send)) { (result) in
            
            GlobalConfig.requestCache.removeValue(forKey: requestKey)
            
            self.dataProtocol(with: result, success: { (data) in
                let page = PageModel.init(json: data)
                let datas = ReadingMeterTaskModel.array(from: data["elements"].arrayValue)

                success(datas,page)
                
            }, overdue: {
                let _ = MeterRequest.shared.getReadingMeterTaskList(send: send, success: success, failure: failure)
                
            }, failure: failure)
            
        }
        GlobalConfig.requestCache[requestKey] = cancle
        
        return cancle
        
    }
    
    //MARK: 获取抄表任务详情
    @discardableResult
    func getReadingMeterTaskDetail(send:ReadingMeterDetailSend, success: @escaping (ReadingMeterTaskDetailModel) -> (), failure: @escaping VoidClosure) -> Cancellable {
        
        let requestKey = SJApp.functionKey()
        if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
        let cancle = MeterProvider.request(MeterAPI.getReadingMeterTaskDetail(send: send)) { (result) in
            
            GlobalConfig.requestCache.removeValue(forKey: requestKey)
            
            self.dataProtocol(with: result, success: { (data) in
                
                let model = ReadingMeterTaskDetailModel.init(json: data)
                success(model)
                
            }, overdue: {
                let _ = MeterRequest.shared.getReadingMeterTaskDetail(send: send, success: success, failure: failure)
                
            }, failure: failure)
            
        }
        
        GlobalConfig.requestCache[requestKey] = cancle
        
        return cancle
        
    }
    
    //MARK: 获取抄表任务详情提交
    @discardableResult
    func submitReadingMeterTaskDetail(send:ReadingMeterDetailSubmitSend, success: @escaping () -> (), failure: @escaping VoidClosure) -> Cancellable {
        
        let requestKey = SJApp.functionKey()
        if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
        let cancle = MeterProvider.request(MeterAPI.submitReadingMeterTaskDetail(send: send)) { (result) in
            
            GlobalConfig.requestCache.removeValue(forKey: requestKey)
            
            self.dataProtocol(with: result, success: { (data) in
                
                success()
                
            }, overdue: {
                let _ = MeterRequest.shared.submitReadingMeterTaskDetail(send: send, success: success, failure: failure)
                
            }, failure: failure)
            
        }
        GlobalConfig.requestCache[requestKey] = cancle
        
        return cancle
        
    }
    
}
